/**
 * 工具包
 */
var tool = {};

//ajax异步请求（查询）
tool.query = function(url, data, success) {
	$.ajax({
		type: "POST",
		url: url,
		data: data,
		dataType: "json",
		success: function(res) {
			if(success != null) {
				success(res);
			}
		},
		error: function(res) {
			new $.zui.Messager(res.responseText, {
				type: 'danger'
			}).show();
		}
	});
};

//ajax异步请求（保存操作）
tool.save = function(url, data, success) {
	$.ajax({
		type: "POST",
		url: url,
		data: data,
		dataType: "json", 
		success: function(res) {
			if(success != null) {
				success(res);
			}
			tool.ok();
		},
		error: function(res) {
			new $.zui.Messager(res.responseText, {
				type: 'danger'
			}).show();
		}
	});
};

tool.ok = function() {
	//首次，初始化
	if(!tool.ok_init) {
		tool.ok_init = true;
		
		var h = 
		'<div id="tool_ok" \
			style="position:fixed; left:46%; top:30%; padding:1em; background-color:#666; color:#fff; border-radius:5px;">\
			<i class="icon icon-check icon-4x"></i>\
		</div>';
		$("body").append(h);
	}
	$("#tool_ok").fadeIn();
	setTimeout(function(){
		$("#tool_ok").fadeOut();
	}, 1200);
}

//设置cookie
tool.setCookie = function(name, value, expiredays) {
	if(expiredays == null) {
		expiredays = 60;
	}
	if(value == null) {
		value = "";
	}
	var exdate = new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	var cookie = name+"="+escape(value)+"; path=/; expires="+exdate.toGMTString();
	document.cookie = cookie;
}

//获取cookie
tool.getCookie = function(name) {
	if(document.cookie.length > 0) {
		c_start = document.cookie.indexOf(name+"=");
		if(c_start != -1) {
			c_start = c_start + name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if(c_end == -1) {
				c_end = document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start, c_end));
		}
	} else {
		return "";
	}
}